package com.bawei.aspect

import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.aspect.annotation.CheckLogin
import com.bawei.cache.CacheManager
import com.bawei.cache.CacheStrategy
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Pointcut
import org.aspectj.lang.reflect.MethodSignature

/**
 * 声明一个切面类
 */
@Aspect
class CheckLoginAspect {

    //定义切点, execution(@注解的全路径包名.注解名称 * *(..))
    @Pointcut("execution(@com.bawei.aspect.annotation.CheckLogin * *(..))")
    fun checkLogin(){

    }

    //自定义切点的处理逻辑
    @Around("checkLogin()")
    fun aroundCheckLogin(proceedingJoinPoint: ProceedingJoinPoint){
        //获取注解传递的参数
        val methodSignature = proceedingJoinPoint.signature as MethodSignature
        val method = methodSignature.method
        //获取注解
        val annotation = method.getAnnotation(CheckLogin::class.java)
        val path = annotation.path
        var jumpPath = "/login/activity"
        if(!path.isNullOrEmpty()){
            jumpPath = path
        }
        //获取Token
        var token =  CacheManager.getCache(CacheStrategy.MMKV).getString("token")
        //未登录
        if (token.isNullOrEmpty()) {
            ARouter.getInstance().build(jumpPath).navigation()
        } else {
            proceedingJoinPoint.proceed()
        }
    }





}